package org.springblade.upgrade.config.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.upgrade.config.model.PackageFrontConfigBean;
import org.springblade.upgrade.config.service.IPackageFrontConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author qp
 */
@Slf4j
@RestController
@RequestMapping("/packageFrontConfig")
public class PackageFrontConfigController extends BladeController {

    @Resource
    private IPackageFrontConfigService packageFrontConfigService;

    /**
     * 新增接口
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody PackageFrontConfigBean param) {
        try {
            if (this.packageFrontConfigService.addData(param)) {
                return R.success("保存成功");
            }
            return R.fail("保存失败");
        } catch (Exception ex) {
            log.error("保存异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 编辑接口
     */
    @PostMapping("/edit")
    public R<String> edit(@RequestBody PackageFrontConfigBean param) {
        try {
            if (this.packageFrontConfigService.updateData(param)) {
                return R.success("修改成功");
            }
            return R.fail("修改失败");
        } catch (Exception ex) {
            log.error("修改异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 删除接口
     */
    @PostMapping("/remove")
    public R<String> remove(PackageFrontConfigBean param) {
        try {
            this.packageFrontConfigService.deleteData(param);
            return R.success("删除成功");
        } catch (Exception ex) {
            log.error("删除异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 查看详情接口
     */
    @GetMapping("/detail")
    public R<PackageFrontConfigBean> detail(PackageFrontConfigBean param) {
        try {
            PackageFrontConfigBean detail = this.packageFrontConfigService.findBySpec(param);
            return R.data(detail);
        } catch (Exception ex) {
            log.error("查询异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 查询分页
     */
    @GetMapping("/page")
    public R<IPage<PackageFrontConfigBean>> list(PackageFrontConfigBean param, Query query) {
        try {
            IPage<PackageFrontConfigBean> page = this.packageFrontConfigService.findPageBySpec(Condition.getPage(query), param);
            return R.data(page);
        } catch (Exception ex) {
            log.error("查询异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }


    /**
     * 查询分页
     */
    @GetMapping("/dicList")
    public R<List<Map<String, Object>>> dicList(PackageFrontConfigBean param) {
        try {
            List<PackageFrontConfigBean> dataList = this.packageFrontConfigService.findListBySpec(param);
            List<Map<String, Object>> list = new ArrayList<>();
            if (dataList != null && !dataList.isEmpty()) {
                Map<String, Object> item;
                for (PackageFrontConfigBean data : dataList) {
                    item = new HashMap<>();
                    item.put("configId", data.getId());
                    item.put("configName", data.getSysModule());
                    list.add(item);
                }
            }
            return R.data(list);
        } catch (Exception ex) {
            log.error("查询异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }


}

